/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.features;

import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import org.osdtsystem.matrixparser.logging.Log;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class UnionFeatureVector extends AbstractFeatureVector {
    List<FeatureVector> baseFeatureVectors;
    FeatureVector featureVectorForWriting;

    public UnionFeatureVector(List<FeatureVector> baseFeatureVectors, FeatureVector featureVectorForWriting) {
        this.baseFeatureVectors = baseFeatureVectors;
        this.featureVectorForWriting = featureVectorForWriting;
    }

    public UnionFeatureVector(List<FeatureVector> baseFeatureVectors) {
        this(baseFeatureVectors, null);
    }

    public double getValue(Feature f) {
        double value = 0;
        for (FeatureVector baseFeatureVector : baseFeatureVectors)
            value += baseFeatureVector.getValue(f);
        return(value);
    }

    public void addValue(Feature feature, double value) {
        if (featureVectorForWriting == null)
            throw new UnsupportedOperationException("Not supported.");
        else {
            double currentWritingValue = featureVectorForWriting.getValue(feature);
            featureVectorForWriting.setValue(feature,
                    value + currentWritingValue);
        }
    }

    public void addValue(FeatureHandler fh, CharSequence chars, double v) {
        addValue(fh.getFeature(chars.toString()), v);
    }


    public void setValue(Feature feature, double value) {
        if (featureVectorForWriting == null)
            throw new UnsupportedOperationException("Not supported.");
        else {
            double currentWritingValue = featureVectorForWriting.getValue(feature);
            double currentTotalValue = getValue(feature);
            featureVectorForWriting.setValue(feature,
                    value - (currentTotalValue - currentWritingValue));
        }
    }

    public int size() {
        int size = 0;
        for (FeatureVector baseFeatureVector : baseFeatureVectors)
            size += baseFeatureVector.size();
        return(size);
    }

    public int checksum() {
        int checksum = 0;
        for (int i = 0; i < baseFeatureVectors.size(); ++i) {
            checksum = checksum * 23 + baseFeatureVectors.get(i).checksum();
        }
        return checksum;
    }

    public String checksum64() {
        return Log.base64(checksum());
    }



    Iterator<Entry<Feature,Double>> subIterator(int featureVectorIndex) {
        if (featureVectorIndex >= baseFeatureVectors.size()) {
            return(null);
        } else {
            return(baseFeatureVectors.get(featureVectorIndex).iterator());
        }
    }

    @Override
    public Iterator<Entry<Feature, Double>> iterator() {
        return(new UnionFeatureVectorIterator(this));
    }


    class UnionFeatureVectorIterator implements Iterator<Entry<Feature,Double>> {
        final UnionFeatureVector unionFeatureVector;
        int nextBaseFeatureVector = 0;
        Iterator<Entry<Feature,Double>> currentIterator;

        private UnionFeatureVectorIterator(UnionFeatureVector unionFeatureVector) {
            this.unionFeatureVector = unionFeatureVector;
            currentIterator = unionFeatureVector.subIterator(nextBaseFeatureVector++);
        }

        public boolean hasNext() {
            while (currentIterator != null) {
                if (currentIterator.hasNext())
                    return(true);
                currentIterator = unionFeatureVector.subIterator(nextBaseFeatureVector++);
            }
            return(false);
        }

        public Entry<Feature,Double> next() {
            if (hasNext()) {
                return(currentIterator.next());
            } else {
                throw new NoSuchElementException("No more elements in union feature vector iterator");
            }
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }
    }

    public void ignoreFeatures(FeatureVector fv) {
        for (FeatureVector fvb : baseFeatureVectors) {
            fvb.ignoreFeatures(fv);
        }
    }

    public void add(FeatureVector fv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
